<?php
/**
 * @author spenceryg
 * @date 2022/12/3
 * @time 3:59 下午
 * @focus love & life
 */


namespace app\auto\controller;


use think\facade\View;
use think\response\Json;

class Controller extends \app\BaseController
{
    /**
     * index
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): string
    {
        $app_id = input('app_id');
        $app = \app\auto\model\App::find($app_id);
        View::assign('data', $app->controller);
        return View::fetch();
    }

    /**
     * add
     * @return string|Json
     */
    public static function add()
    {
        if(request()->isPost()){
            $data = input();
            validate([
                'app_id|应用ID' => 'require',
                'name|控制器名称' => 'require',
                'comment|控制器中文名' => 'require'
            ])->check($data);
            $find = \app\auto\model\Controller::where('app_id', $data['app_id'])
                ->where('name', $data['name'])->find();
            if($find) {
                return error('控制器已存在!');
            }
            \app\auto\model\Controller::create($data);
            return success();
        } else {
            return View::fetch();
        }


    }

    /**
     * delete
     * @return Json
     */
    public static function delete(): Json
    {
        $id = input('id');
        \app\auto\model\Controller::destroy($id);
        return success();
    }


}
